###########################
#
#Script to Code variables
#
###########################

#Clear R
rm(list=ls())

#load Kam_data_archive.Rdta
load("Kam_data_archive.RData")

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("car", "psych","moments")
ipak(packages)

###########################
#Create 18-item NFC measure
###########################

#reversed coded itmes
data$NfC_3_rec<-(6-data$NfC_3)
data$NfC_4_rec<-(6-data$NfC_4)
data$NfC_5_rec<-(6-data$NfC_5)
data$NfC_7_rec<-(6-data$NfC_7)
data$NfC_8_rec<-(6-data$NfC_8)
data$NfC_9_rec<-(6-data$NfC_9)
data$NfC_12_rec<-(6-data$NfC_12)
data$NfC_16_rec<-(6-data$NfC_16)
data$NfC_17_rec<-(6-data$NfC_17)

#create 18-item scale
data$NFC18<-rowMeans(data.frame(data$NfC_1+ data$NfC_2+ data$NfC_3_rec+ data$NfC_4_rec+ data$NfC_5_rec+ data$NfC_6+ data$NfC_7_rec+ data$NfC_8_rec+ data$NfC_9_rec+ data$NfC_10+ data$NfC_11+ data$NfC_12_rec+ data$NfC_13+ data$NfC_14+ data$NfC_15+ data$NfC_16_rec+ data$NfC_17_rec+ data$NfC_18), na.rm=T)

#recode to range from 0 - 1
data$NFC18<-((data$NFC18-18)/72)

##########################
#6-items from Bullock
##########################
#1.	I prefer complex to simple problems.
#2.	I like to have the responsibility of handling a situation that requires a lot of thinking.
#3.	Thinking is not my idea of fun.
#6.	I find satisfaction in deliberating hard and for long hours.
#8.	I prefer to think about small daily projects to long term ones.
#16.	I feel relief rather than satisfaction after completing a task that requires a lot of mental effort.

#Create scale
data$NFC_Bullock<-rowMeans(data.frame(data$NfC_1+ data$NfC_2+ data$NfC_3_rec+ data$NfC_6+ data$NfC_16_rec+ data$NfC_8_rec), na.rm=T)
data$NFC_Bullock<-((data$NFC_Bullock-6)/24)

###########################
#2-item measure
###########################
#NFC Item 1: I like to have the responsibility of handling a situation that requires a lot of thinking.
#NFC Item 2: I would prefer complex to simple problems.9

#create 2-item scale
data$NFC_ANES<-rowMeans(data.frame(data$NfC_1+ data$NfC_2), na.rm=T)
#code to range from 0 to 1
data$NFC_ANES<-((data$NFC_ANES-2)/8)

############################
#
#Dependent variable: scored so that high values are support for the ban
#
###########################

#Item 1 (Kam: support for irradiation)
#Recode so that opposition is the highest value
data$SupportIrradiation_rec<-(6-data$SupportIrradiation)

#Item 2: Costs outweigh benefits
#recode so that opposition is the highest value
data$CostsFoodIrradiation_rec<-(6-data$CostsFoodIrradiation)

#Item 3: food irradiation is bad
#recode so that opposition is the highest value
data$IrradiationGood_rec<-(6-data$IrradiationGood)

#create scale: high score is ban irradiation
data$DV_irradiation<-rowMeans(data.frame(data$SupportIrradiation_rec+ data$CostsFoodIrradiation_rec+ data$IrradiationGood_rec), na.rm=T)
#recode to range from 0 to 1
data$DV_irradiation<-((data$DV_irradiation-3)/12)

####################################
#indicator of the treatment
####################################
data$Treatment <- NA
data$Treatment[data$T_Control==1]=1
data$Treatment[data$T_DemProp_RepOp==1]=2
data$Treatment[data$T_RepProp_DemOp==1]=3

#####################################
#Moderation of the NfC
######################################

#PID1: 1==Rep, 2==Dem, 3==Independent; 4 something else
table(data$pid1)
#PID4: leaners -> 1==leaning Rep; 2==leaning Dem; 3== not
table(data$pid4)

#In-party
# In-party endorsement: 0 (out-party endorses ban or no party cues); 1 (leaning, weak or strong partisan, in-party endorses ban). 
#Inparty endorsement
data$InParty<-NA
data$InParty[(data$Treatment==2 & data$pid1==1) | (data$Treatment==2 & data$pid4==1) | (data$Treatment==3 & data$pid1==2 )| (data$Treatment==3 & data$pid4==2 )|data$Treatment==1]=0
data$InParty[(data$Treatment==2 & data$pid1==2 )| (data$Treatment==3 & data$pid1==1 )]=1
data$InParty[(data$Treatment==2 & data$pid4==2 )| (data$Treatment==3 & data$pid4==1) ]=1

#Out-party
#Out-party endorsement: 0 (in-party endorses ban or no party cues); 1 (leaning partisan; weak or strong partisan, out-party endorses ban).
#outparty
data$OutParty<-NA
data$OutParty[data$Treatment==2 & data$pid1==2 | data$Treatment==2 & data$pid4==2 | data$Treatment==3 & data$pid1==1 | data$Treatment==3 & data$pid4==1 | data$Treatment==1]=0
data$OutParty[data$Treatment==2 & data$pid1==1 | data$Treatment==3 & data$pid1==2 ]=1
data$OutParty[data$Treatment==2 & data$pid4==1 | data$Treatment==3 & data$pid4==2 ]=1

#No-Cues
data$NoCues<-ifelse(data$Treatment==1,1,0)

#*************************
#
#Covariates
#
#*************************

#gender: male==0, female==1
data$female[data$Q110==1]<-0
data$female[data$Q110==2]<-1

#Age
data$age<-(2016-as.numeric(data$Q116, na.rm=T))

#Race
table(data$race)

#Education
data$education<-data$hs

#Income
data$income<-data$Q114

#Select only those observations that participated in the survey
data <- subset(data, Treatment > 0.1)

#save in a specified directory
save(data, file="Kam_data.RData")

